<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">角色管理</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="selectForm" class="form-inline" th:action="@{/role/list}" role="form">
            <div class="form-group">
                <div class="form-group">
                    <label class="control-label">角色名称：</label>
                    <div class="control-inline">
                        <input type="text" name="roleName" class="form-control" />
                    </div>
                </div>

                <shiro:hasPermission name="role:list">
                    <div class="btn-group " style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                        <button type="reset" id="btn_reset" class="btn btn-warning">重置</button>
                    </div>
                </shiro:hasPermission>
            </div>
        </form>
    </div>
    <div class="jax-box jax-box-table">
        <div id="toolbar" class="toolbar btn-group">
            <shiro:hasPermission name="role:add">
                <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" data-target="#roleModal">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="role:batchDelete">
                <button id="btn_batch_delete" type="button" class="btn btn-danger">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除
                </button>
            </shiro:hasPermission>
        </div>


        <div class="wrapper-content">
            <div class="ibox-content animated fadeInRight">
                <div class="jqGrid_wrapper">
                    <table id="table_list"></table>
                    <div id="pager_list"></div>
                </div>
            </div>
        </div>

    </div>
</section>

<!-- 角色模态框 -->
<div id="roleModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">新增角色</h4>
            </div>
            <div class="modal-body">
                <div>
                    <form class="form-horizontal" id="roleForm" method = 'post' th:action="@{/role/add}">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="roleName" class="col-sm-3 control-label">角色名称 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="roleName" id="roleName" require="必填项"  placeholder="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="remark" class="col-sm-3 control-label">角色描述 :</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" name="remark" id="remark" placeholder="">
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <div class="pull-right">
                                <button type="button" id="saveRole" class="btn btn-info">确定</button>
                                <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                            </div>
                        </div>
                        <!-- /.box-footer -->
                    </form>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<!-- 角色详情模态框 -->
<div id="roleDetailModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">角色信息</h4>
            </div>
            <div class="modal-body">
                <div id="roleOpenWindow">
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<!-- 分配权限 -->
<div id="assignPermissionModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">分配权限</h4>
            </div>
            <div class="modal-body">
                <ul id="permissionTree" class="ztree"></ul>
            </div>
            <div class="modal-footer">
                <div class="pull-right">
                    <button type="button" id="saveAssign" class="btn btn-info">确定</button>
                    <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<script type="text/javascript">

    var roleId="";
    var roleIdStr;//批量

    //树-初始化设置
    var treeSetting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId"
            }
        }
    };


    $(document).ready(function () {
        var editFlag = "[[${@perms.hasPerm('role:edit')}]]";
        var deleteFlag = "[[${@perms.hasPerm('role:delete')}]]";
        var assignMenusFlag="[[${@perms.hasPerm('role:assignMenus')}]]";

        var dataGrid = new DataGrid({
            dataGrid: $('#table_list'),
            searchForm: $('#selectForm'),
            dataGridPage: $('#pager_list'),
            multiselect: true,
            multiboxonly: true,
            rownumbers: true,
            rownumWidth: 60,
            colModel: [
                {
                    header: 'ID',
                    name: 'roleId',
                    index: 'roleId',
                    editable: false,
                    width: 40,
                    align: 'center',
                    sortable: false,
                    search: false
                },
                {
                    header: '角色名称',
                    name: 'roleName',
                    index: 'roleName',
                    width: 30,
                    sortable: false,
                    align: 'center'
                },
                {
                    header: '角色描述',
                    name: 'remark',
                    index: 'remark',
                    width: 30,
                    sortable: false,
                    align: 'center'
                },
                {
                    header: '创建时间',
                    name: 'createTime',
                    index: 'createTime',
                    width: 30,
                    sortable: false,
                    align: 'center'
                },
                {
                    header: '操作',
                    name: 'operate',
                    width: 80,
                    align: 'center',
                    sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        // var edit = editFlag == "true" ? '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editRole(' + rowObject.roleId + ')">编辑</a>' : '';
                        // var assign = assignMenusFlag == "true" ? '<a class="table-btn table-btn-info"  href="javascript:void(0)" onclick="assignMenusList(' + rowObject.roleId + ')">分配权限</a>' : '';
                        // var del = deleteFlag == "true" ? '<a  class="table-btn table-btn-danger" href="javascript:void(0)" onclick="deleteRole(' + rowObject.roleId + ')">删除</a>' : '';

                        var edit = '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editRole(' + rowObject.roleId + ')">编辑</a>' ;
                        var assign = '<a class="table-btn table-btn-info"  href="javascript:void(0)" onclick="assignMenusList(' + rowObject.roleId + ')">分配权限</a>' ;
                        var del = '<a  class="table-btn table-btn-danger" href="javascript:void(0)" onclick="deleteRole(' + rowObject.roleId + ')">删除</a>';
                        return edit + assign + del;
                    }
                }
            ]
        });

        //加载表格
        $("#btn_query").click(function () {
            $("#table_list").jqGrid('setGridParam', {
                datatype: 'json',
                postData: {
                    userName: $('input[name=roleName]').val()
                }, // 发送数据
                page: 1
            }).trigger("reloadGrid"); //重新载入
        });

        //新增-保存
        $("#saveRole").click(function(){
            if(doValidForm(roleForm)){
                Core.mask("#saveRole");
                Core.postAjax(ctxPath + "/role/add",$("#roleForm").serialize(),function (data) {
                    Core.unmask("#saveRole");
                    if(data.code==200){
                        $("#roleModal").modal("hide");
                        $("#roleForm")[0].reset();
                        Core.refreshJqTable("#table_list");
                    }
                    layer.msg(data.msg);
                })
            };
        });

        //批量删除
        $("#btn_batch_delete").click(function(){
            var checkedRows= Core.selectJqMutiData("#table_list");
            if(checkedRows!=false){
                Core.confirm("确定删除选中的"+checkedRows.length+"条记录？",function () {
                    roleIdStr="";
                    $.each(checkedRows, function (i, item) {
                        var rowData = $("#table_list").jqGrid('getRowData', checkedRows[i]);
                        roleIdStr+=(rowData.roleId+",");
                    });
                    roleIdStr=roleIdStr.substring(0,roleIdStr.length-1);
                    Core.postAjax(ctxPath + "/role/batch/delete",{"roleIdStr":roleIdStr},function(data){
                        if(data.code==200){
                            Core.refreshJqTable("#table_list");
                        }
                        layer.msg(data.msg);
                    },"get")
                })
            }
        });

        /*保存分配*/
        $("#saveAssign").click(function () {
            Core.mask("#saveAssign")
            var menuIdStr="";
            var treeObj = $.fn.zTree.getZTreeObj("permissionTree");
            var nodes = treeObj.getCheckedNodes(true);
            for(var i=0;i<nodes.length;i++){
                if(i==(nodes.length-1)){
                    menuIdStr+=nodes[i].menuId;
                }else{
                    menuIdStr+=nodes[i].menuId+",";
                }
            }
            Core.postAjax(ctxPath + '/role/assign/menu',{roleId:roleId,menuIdStr:menuIdStr},function (data) {
                Core.unmask("#saveAssign");
                if(data.code==200){
                    $("#assignPermissionModal").modal("hide");
                }
                layer.msg(data.msg);
            });
        });

    });

    /*删除*/
    function deleteRole(roleId) {
        Core.confirm("确定删除该角色？",function () {
            Core.postAjax(ctxPath + "/role/delete", {"roleId": roleId}, function (data) {
                if (data.code == 200) {
                    Core.refreshJqTable("#table_list");
                }
                layer.msg(data.msg);
            }, "get")
        })
    }

    /*编辑*/
    function editRole(roleId) {
        Core.load("#roleOpenWindow", ctxPath + "/role/edit?&roleId=" + roleId, function () {
            $("#roleDetailModal").modal("show");
        }, 2);
    }

    /*分配权限-列表查询*/
    function assignMenusList(id) {
        roleId=id;
        Core.postAjax(ctxPath + "/role/assign/menu/list",{"roleId":id},function(data){
            var zNodes=data;
            $.fn.zTree.init($("#permissionTree"), treeSetting, zNodes);
            $("#assignPermissionModal").modal("show");
        })
    }
</script>

